<template>
	<view class="applyInfo">
		<u-navbar :title="title" back-icon-color="#fff"
			:background="{backgroundImage: 'linear-gradient(45deg, #68dae7, #21a4b3)'}" title-color="#FFFFFF">
		</u-navbar>
		<view class="applyInfo-form">
			<u-form :model="checkInfo" label-width="180rpx" >
				<u-form-item label="序列号"><u-input v-model="checkInfo.serialNumber" disabled/></u-form-item>
				<u-form-item label="仪器名称"><u-input v-model="checkInfo.deviceName" placeholder=" " disabled/></u-form-item>
				<u-form-item :label="labelCheck"><u-input v-model="checkForm.people" placeholder=" " disabled/></u-form-item>
				<u-form-item :label="labelProcess">
					<u-input type="textarea" :height="240" v-model="checkForm.process" placeholder=" "/>
				</u-form-item>
			</u-form>
			<view class="applyInfo-img" >
				<view>{{repotLable}}</view>
				<u-upload ref="uUpload" :max-count="1" :action="action"  @on-success="uploadSuccess" @on-error="uploadError" :header="uploadHeader" :auto-upload="false" ></u-upload>
			</view>
		</view>
		<view class="applyInfo-submit">
			<view class="applyInfo-submit-btn" @tap="saveApply">确定</view>
		</view>
		<xt-loading :showMask="showMask"></xt-loading>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				checkInfo:{},
				checkForm:{
					people:uni.getStorageSync("userName")
				},
				title:"",
				labelCheck:"",
				labelProcess:"",
				repotLable:"",
				action:uni.getStorageSync("baseUrl")+"/wx/upload",
				uploadHeader:{
					"Content-Type": "multipart/form-data",
					"token":uni.getStorageSync("token"),
				},
				showMask:false
			}
		},
		onLoad(options) {
			this.checkInfo = options
			if(this.checkInfo.cycleType == 1){
				this.title = "仪器巡检"
				this.labelCheck = "巡检人"
				this.labelProcess = "巡检过程"
				this.repotLable = "巡检报告"
			}else if(this.checkInfo.cycleType == 2){
				this.title = "仪器核查"
				this.labelCheck = "核查人"
				this.labelProcess = "核查过程"
				this.repotLable = "核查报告"
			}else{
				this.title = "仪器保养"
				this.labelCheck = "保养人"
				this.labelProcess = "保养过程"
				this.repotLable = "保养报告"
			}
			console.log(options)
		},
		methods:{
			saveApply(){
				if(this.$refs.uUpload.lists.length>0){
					this.showMask = true
					this.$refs.uUpload.upload()
				}else{
					this.showMask = true
					this.saveInfo()
				}
			},
			uploadSuccess(data, index, lists, name){
				if(this.checkInfo.cycleType == 1){
					this.checkInfo.inspectedPic = data.url
					this.saveInfo()
				}else if(this.checkInfo.cycleType == 2){
					this.checkInfo.checkPic = data.url
					this.saveInfo()
				}else{
					this.checkInfo.mainPic = data.url
					this.saveInfo()
				}
			},
			uploadError(res){
				this.showMask = false
				this.$refs.uToast.show({
					title:res.msg,
					duration:2000,
					type:"error"
				})
			},
			saveInfo(){
				if(this.checkInfo.cycleType == 1){
					this.checkInfo.inspectedBy = this.checkForm.people
					this.checkInfo.inspectedTime = this.$xt.util.getNowTime()
					this.checkInfo.inspectedRecord = this.checkForm.process
					this.$xt.api.inspectAdd(this.checkInfo).then(res=>{
						this.showMask = false
						this.$refs.uToast.show({
							title:res.msg,
							duration:2000,
							type:"success"
						})
						this.goBack()
					}).catch(res=>{
						this.showMask = false
						this.$refs.uToast.show({
							title:res.msg,
							duration:2000,
							type:"error"
						})
					})
				}else if(this.checkInfo.cycleType == 2){
					this.checkInfo.checkBy = this.checkForm.people
					this.checkInfo.checkTime = this.$xt.util.getNowTime()
					this.checkInfo.checkRecord = this.checkForm.process
					this.$xt.api.checkAdd(this.checkInfo).then(res=>{
						this.showMask = false
						this.$refs.uToast.show({
							title:res.msg,
							duration:2000,
							type:"success"
						})
						this.goBack()
					}).catch(res=>{
						this.showMask = false
						this.$refs.uToast.show({
							title:res.msg,
							duration:2000,
							type:"error"
						})
					})
				}else{
					this.checkInfo.mainBy = this.checkForm.people
					this.checkInfo.mainTime = this.$xt.util.getNowTime()
					this.checkInfo.mainParts = this.checkForm.process
					this.$xt.api.mainAdd(this.checkInfo).then(res=>{
						this.showMask = false
						this.$refs.uToast.show({
							title:res.msg,
							duration:2000,
							type:"success"
						})
						this.goBack()
					}).catch(res=>{
						this.showMask = false
						this.$refs.uToast.show({
							title:res.msg,
							duration:2000,
							type:"error"
						})
					})
				}
			},
			goBack(){
				let page = getCurrentPages(),
				currentPages = page[page.length - 2]
				setTimeout(()=>{
					currentPages.$vm.reloadList()
					this.$u.route({type: 'back'})
				},1000)
			}
		}
	}
</script>

<style scoped lang="scss">
	
	.applyInfo{
		position: relative;
		&-form{
			background-color: #fff;
			padding: 10rpx 24rpx;
		}
		&-submit{
			position: fixed;
			bottom: 10rpx;
			width: 100%;
			background-color: #fff;
			padding: 20rpx 40rpx;
			&-btn{
				text-align: center;
				background-color: #00aa00;
				color:#fff;
				padding: 10rpx 40rpx;
				border-radius: 15rpx;
			}
		}
	}
</style>